import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex) // vue的插件机制

// Vuex.Store 构造器选项
const store = new Vuex.Store({
    // 为了不和页面或组件的data中的造成混淆，state中的变量前面建议加上$符号
    state: {
		// 多账号信息
		userList: uni.getStorageSync('userList') || [],
        // 用户信息
        userInfo: uni.getStorageSync('userInfo') || {},
		token: uni.getStorageSync('token') || '',
		// 是否登录
		isLogin: uni.getStorageSync('token') ? true : false,
		// 发音类型：RP=英式发音，GA=美式发音
		voiceType: uni.getStorageSync('voiceType') ? uni.getStorageSync('voiceType') : 'RP',
		// 测试类型：1=顺序，2=随机
		testType: uni.getStorageSync('testType') ? uni.getStorageSync('testType') : 2,
		// 公共数据
		commonData: uni.getStorageSync('commonData') || [],
		// 当前学习教材
		currentBookId: uni.getStorageSync('currentBookId') || 0,
		// 当前学习单元
		currentUnitId: uni.getStorageSync('currentUnitId') || 0,
    },
	mutations: {
		// 登录
		SET_LOGIN(state, payload) {
			if (payload) {
				uni.setStorageSync('token', payload.token)
				uni.setStorageSync('userInfo', payload)
				state.userInfo = payload
				state.token = payload.token
			}
			state.isLogin = true
			// 缓存多账号信息
			payload.is_current = 1
			let userList = state.userList
			let isExist = false
			if (userList.length > 0) {
				userList.forEach((user) => {
					if (user.id == payload.id) {
						isExist = true
						user = Object.assign(user, payload)
					} else {
						user.is_current = 0
					}
				})
			}
			if (!isExist) {
				userList.push(payload)
			}
			uni.setStorageSync('userList', userList)
		},
		// 退出登录
		SET_LOGOUT(state) {
			state.userInfo = {}
			state.token = ""
			state.isLogin = false
			uni.removeStorageSync('token')
			uni.removeStorageSync('userInfo')
		},
		// 设置发音类型
		SET_VOICETYPE(state, payload) {
			uni.setStorageSync('voiceType', payload)
			state.voiceType = payload
		},
		// 设置测试类型
		SET_TESTTYPE(state, payload) {
			uni.setStorageSync('testType', payload)
			state.testType = payload
		},
		// 公共数据
		updateCommonData(state, payload) {
			uni.setStorageSync('commonData', payload.commonData)
			state.commonData = payload.commonData
		},
		// 多账号删除
		USER_DELETE(state, payload) {
			let userList = state.userList
			userList.splice(payload, 1)
			uni.setStorageSync('userList', userList)
			state.userList = userList
		},
	},
	actions: {
		// 登录操作
		userLogin({ commit }, userInfo) {
			commit('SET_LOGIN', userInfo)
		},
		// 登出操作
		userLogout({ commit }) {
			commit('SET_LOGOUT')
		},
		// 设置发音类型
		voiceType({ commit }, type) {
			commit('SET_VOICETYPE', type)
		},
		// 设置测试类型
		testType({ commit }, type) {
			commit('SET_TESTTYPE', type)
		},
		// 获取公共数据
		getCommonData({ commit, state }) {
			let baseUrl = getApp().globalData.baseUrl
			let Token = state.token
			uni.request({
				url: baseUrl + '/api/index/common',
				method: 'post',
				data: {},
				header: { token: Token },
				success: (result) => {
					if (result.data.code == 1) {
						commit('updateCommonData', {
							commonData: result.data.data
						})
					} else {
						uni.$u.toast(result.data.msg)
					}
				},
				fail: (error) => {
					console.log(error)
				}
			})
		},
		// 多账号管理
		userDelete({ commit }, index) {
			commit('USER_DELETE', index)
		},
	},
	getters: {
		//判断vip会员是否过期
		isVip: state => {
			if (state.userInfo.is_vip) {
				//已购买过vip
				if (state.userInfo.vip_end_time > 0 && state.userInfo.vip_end_time * 1000 < new Date().getTime()) {
					return false
				} else {
					return true
				}
			} else {
				//试用期
				if (state.userInfo.is_try == 1) {
					if (state.userInfo.try_end_time * 1000 < new Date().getTime()) {
						return false
					} else {
						return true
					}
				} else {
					return false
				}
			}
		}
	}
})

export default store
